//
//  DetailViewController.m
//  Shanghai Cuisine
//
//  Created by iOS Two on 14-9-10.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "DetailViewController.h"
#import "UIImageView+MJWebCache.h"
#import "myData.h"
#import "myObj.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated {
    [MobClick beginLogPageView:@"DetailViewController"];
}
-(void)viewDidAppear:(BOOL)animated {
    [MobClick endLogPageView:@"DetailViewController"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self creatController];
    UILabel *titelLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 44)];
    titelLabel.text = @"菜式详解";
    titelLabel.textAlignment = NSTextAlignmentCenter;
    titelLabel.font = [UIFont systemFontOfSize:19];
    titelLabel.textColor = [UIColor colorWithRed:0.97 green:0.69 blue:0.17 alpha:1];
    self.navigationItem.titleView = titelLabel;
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.9 green:0.64 blue:0.16 alpha:1];
    
    myData *mydata=[[myData alloc]init];
    [mydata initData];
    NSArray *array = [mydata selectData];
    if (array.count == 0) {
        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"img_favor1"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
        self.navigationItem.rightBarButtonItem = right;
        _isExist = NO;
    } else {
        for (int i = 0; i < array.count; i++) {
            myObj *obj = [array objectAtIndex:i];
            if ([obj.nameString isEqualToString:_name]) {
                UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"img_favor2"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
                self.navigationItem.rightBarButtonItem = right;
                _isExist = YES;
                return;
            } else {
                UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"img_favor1"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
                self.navigationItem.rightBarButtonItem = right;
                _isExist = NO;
            }
        }
    }
    // Do any additional setup after loading the view.
}
-(void)buttonClick {
    [MobClick event:@"favorButton"];

    myData *mydata=[[myData alloc]init];
    [mydata initData];
    if (!_isExist) {
        [mydata insertData:_name title:_bigImg info:_tips tableName:@"dishName" introduction:_introduction smallImg:_smallImg];
        for (int i = 0; i < _makeImg.count; i++) {
            [mydata insertData:_name title:[_makeImg objectAtIndex:i] info:[_makeInfo objectAtIndex:i] tableName:@"howToDo" introduction:nil smallImg:nil];
            UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"img_favor2"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
            self.navigationItem.rightBarButtonItem = right;
        }
        for (int i = 0; i < _materialArray.count; i++) {
            [mydata insertData:_name title:[_materialArray objectAtIndex:i] info:[_materialQuantity objectAtIndex:i] tableName:@"mainMaterial" introduction:nil smallImg:nil];
        }
        for (int i = 0; i < _auxiliaryMaterial.count; i++) {
            [mydata insertData:_name title:[_auxiliaryMaterial objectAtIndex:i] info:[_auxiliaryQuantity objectAtIndex:i] tableName:@"auxiliaryMaterial" introduction:nil smallImg:nil];
        }
        [self alertViewShow:@"收藏成功"];
        _isExist = YES;
    } else {
        [mydata deleteTestList:_name tableName:@"dishName"];
        [mydata deleteTestList:_name tableName:@"howToDo"];
        [mydata deleteTestList:_name tableName:@"mainMaterial"];
        [mydata deleteTestList:_name tableName:@"auxiliaryMaterial"];
        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"img_favor1"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
        self.navigationItem.rightBarButtonItem = right;
        _isExist = NO;
        [self alertViewShow:@"取消收藏"];
    }
}
-(void)getData {
    [self showIndeterminateProgress];
    
    _makeImg = [[NSMutableArray alloc]init];
    _makeInfo = [[NSMutableArray alloc]init];
    AVQuery *query = [[AVQuery alloc]initWithClassName:@"howToMake"];
    [query whereKey:@"dishName" equalTo:_dishId];
    [query orderByAscending:@"sort"];
    query.cachePolicy = kPFCachePolicyNetworkElseCache;
    query.maxCacheAge = 24 * 3600 * 30;//缓存时间有效期
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        for (int i = 0; i < objects.count; i++) {
            [_makeInfo addObject:[[objects objectAtIndex:i] objectForKey:@"makeInfo"]];
            [_makeImg addObject:[[objects objectAtIndex:i] objectForKey:@"makeImg"]];
        }
        _startTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 32 + _image4.frame.origin.y , 320, _makeInfo.count * 70)];
        _startTable.scrollEnabled = NO;
        _startTable.delegate = self;
        _startTable.dataSource = self;
        _startTable.tag = 2;
        _startTable.rowHeight = 70;
        _startTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_scroll addSubview:_startTable];
        
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, _startTable.frame.origin.y + 16 + _startTable.frame.size.height - 1, 320, 32)];
        image.image = [UIImage imageNamed:@"img_xiaotieshi"];
        [_scroll addSubview:image];
        
        UILabel *tips = [[UILabel alloc]initWithFrame:CGRectMake(10,15 + image.frame.origin.y + image.frame.size.height, 300, 100)];
        tips.text = [[NSString stringWithFormat:@"%@", _tips] stringByReplacingOccurrencesOfString:@"\\n" withString:@" \r\n"];
        tips.numberOfLines = 0;
        tips.font = [UIFont systemFontOfSize:14];
        [tips sizeToFit];
        [_scroll addSubview:tips];
        
        UIImageView *back = [[UIImageView alloc]initWithFrame:CGRectMake(0,15 + tips.frame.size.height + tips.frame.origin.y, 320, 25)];
        back.image = [UIImage imageNamed:@"img_back"];
        [_scroll addSubview:back];
        
        UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(0, back.frame.size.height + back.frame.origin.y, 320, kHeight)];
        background.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
        [_scroll addSubview:background];
        
        _scroll.contentSize = CGSizeMake(320, back.frame.size.height + back.frame.origin.y + 64);
        [HUD hide:YES];
    }];
}
-(void)creatController {
    _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, kHeight)];
    _scroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scroll];
    
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(174, 50, 200, 30)];
    name.font = [UIFont systemFontOfSize:22];
    name.text = _name;
    [name sizeToFit];
    [_scroll addSubview:name];
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 150, 110)];
    [image setImageURLStr:_bigImg placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
    [_scroll addSubview:image];
    
    UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 130, 320, 32)];
    image1.image = [UIImage imageNamed:@"img_jianjie"];
    [_scroll addSubview:image1];
    
    UILabel *introduction = [[UILabel alloc]initWithFrame:CGRectMake(10, 172, 300, 20)];
    introduction.text = [[NSString stringWithFormat:@"%@", _introduction] stringByReplacingOccurrencesOfString:@"\\n" withString:@" \r\n"];;
    introduction.font = [UIFont systemFontOfSize:13];
    introduction.numberOfLines = 0;
    [introduction sizeToFit];
    [_scroll addSubview:introduction];

    UIImageView *image2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, introduction.frame.size.height + introduction.frame.origin.y + 10, 320, 32)];
    image2.image = [UIImage imageNamed:@"img_zhuliao"];
    [_scroll addSubview:image2];
    
    _materialTable = [[UITableView alloc]initWithFrame:CGRectMake(0, image2.frame.size.height + image2.frame.origin.y, 320, 200)];
    if (_materialArray.count % 2 == 1) {
        _materialTable.frame = CGRectMake(0, image2.frame.size.height + image2.frame.origin.y, 320, (_materialArray.count / 2 + 1) * 50 );
    } else {
        _materialTable.frame = CGRectMake(0, image2.frame.size.height + image2.frame.origin.y, 320, (_materialArray.count / 2) * 50 );
    }
    _materialTable.scrollEnabled = NO;
    _materialTable.delegate = self;
    _materialTable.dataSource = self;
    _materialTable.tag = 1;
    _materialTable.rowHeight = 50;
    [_scroll addSubview:_materialTable];

    UIImageView *image3 = [[UIImageView alloc]initWithFrame:CGRectMake(0, _materialTable.frame.origin.y + _materialTable.frame.size.height - 1, 320, 32)];
    image3.image = [UIImage imageNamed:@"img_fuliao"];
    [_scroll addSubview:image3];
    
    _auxiliaryTable = [[UITableView alloc]initWithFrame:CGRectMake(0, image3.frame.origin.y + image3.frame.size.height, 320, 200)];
    if (_auxiliaryMaterial.count % 2 == 1) {
        _auxiliaryTable.frame = CGRectMake(0, image3.frame.origin.y + image3.frame.size.height, 320, (_auxiliaryMaterial.count / 2 + 1) * 50 );
    } else {
        _auxiliaryTable.frame = CGRectMake(0, image3.frame.origin.y + image3.frame.size.height, 320, (_auxiliaryMaterial.count / 2) * 50 );
    }
    _auxiliaryTable.scrollEnabled = NO;
    _auxiliaryTable.delegate = self;
    _auxiliaryTable.dataSource = self;
    _auxiliaryTable.tag = 3;
    _auxiliaryTable.rowHeight = 50;
    [_scroll addSubview:_auxiliaryTable];
    
    _image4 = [[UIImageView alloc]initWithFrame:CGRectMake(0, _auxiliaryTable.frame.size.height + _auxiliaryTable.frame.origin.y - 1, 320, 32)];
    _image4.image = [UIImage imageNamed:@"img_zhizuo"];
    [_scroll addSubview:_image4];
    [self getData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 1) {
        if (_materialArray.count % 2 == 1) {
            return _materialArray.count / 2 + 1;
        } else {
            return _materialArray.count / 2;
        }
    } else if (tableView.tag == 2) {
        return _makeImg.count;
    } else if (tableView.tag == 3) {
        if (_auxiliaryMaterial.count % 2 == 1) {
            return _auxiliaryMaterial.count / 2 + 1;
        } else {
            return _auxiliaryMaterial.count / 2;
        }
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView.tag == 1) {
        if (_materialArray.count % 2 == 1) {
            if (indexPath.row == _materialArray.count / 2) {
                UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
                material1.text = [_materialArray objectAtIndex:indexPath.row * 2];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material1];
                
                UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 70, 30)];
                quantity1.text = [_materialQuantity objectAtIndex:indexPath.row * 2];
                quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                quantity1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:quantity1];
            } else {
                UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
                material1.text = [_materialArray objectAtIndex:indexPath.row * 2];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material1];
                
                UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 70, 30)];
                quantity1.text = [_materialQuantity objectAtIndex:indexPath.row * 2];
                quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                quantity1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:quantity1];
                
                UILabel *material2 = [[UILabel alloc]initWithFrame:CGRectMake(184, 10, 70, 30)];
                material2.text = [_materialArray objectAtIndex:indexPath.row * 2 + 1];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material2];
                
                UILabel *quantity2 = [[UILabel alloc]initWithFrame:CGRectMake(251, 10, 70, 30)];
                quantity2.text = [_materialQuantity objectAtIndex:indexPath.row * 2 + 1];
                quantity2.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                quantity2.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:quantity2];
            }
        } else {
            UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
            material1.text = [_materialArray objectAtIndex:indexPath.row * 2];
            material1.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:material1];
            
            UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 70, 30)];
            quantity1.text = [_materialQuantity objectAtIndex:indexPath.row * 2];
            quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
            quantity1.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:quantity1];
            
            UILabel *material2 = [[UILabel alloc]initWithFrame:CGRectMake(184, 10, 70, 30)];
            material2.text = [_materialArray objectAtIndex:indexPath.row * 2 + 1];
            material2.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:material2];
            
            UILabel *quantity2 = [[UILabel alloc]initWithFrame:CGRectMake(251, 10, 70, 30)];
            quantity2.text = [_materialQuantity objectAtIndex:indexPath.row * 2 + 1];
            quantity2.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
            quantity2.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:quantity2];
        }
    } else if (tableView.tag == 2) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(7, 5, 80, 60)];
        [image setImageURLStr:[_makeImg objectAtIndex:indexPath.row] placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
        image.clipsToBounds = YES;
        image.contentMode = UIViewContentModeScaleAspectFill;
        [cell.contentView addSubview:image];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(92, 5, 215, 55)];
        label.text = [NSString stringWithFormat:@"%d.%@",indexPath.row + 1,[_makeInfo objectAtIndex:indexPath.row]];
        label.font = [UIFont systemFontOfSize:14];
        label.numberOfLines = 4;
        [cell.contentView addSubview:label];
    } else if (tableView.tag == 3) {
        if (_auxiliaryMaterial.count % 2 == 1) {
            if (indexPath.row == _auxiliaryMaterial.count / 2) {
                UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
                material1.text = [_auxiliaryMaterial objectAtIndex:indexPath.row * 2];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material1];
                
                UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(94, 10, 70, 30)];
                quantity1.text = [_auxiliaryQuantity objectAtIndex:indexPath.row * 2];
                quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                quantity1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:quantity1];
            } else {
                UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
                material1.text = [_auxiliaryMaterial objectAtIndex:indexPath.row * 2];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material1];
                
                UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(94, 10, 70, 30)];
                quantity1.text = [_auxiliaryQuantity objectAtIndex:indexPath.row * 2];
                quantity1.font = [UIFont systemFontOfSize:15];
                quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                [cell.contentView addSubview:quantity1];
                
                UILabel *material2 = [[UILabel alloc]initWithFrame:CGRectMake(186, 10, 70, 30)];
                material2.text = [_auxiliaryMaterial objectAtIndex:indexPath.row * 2 + 1];
                material2.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material2];
                
                UILabel *quantity2 = [[UILabel alloc]initWithFrame:CGRectMake(251, 10, 70, 30)];
                quantity2.text = [_auxiliaryQuantity objectAtIndex:indexPath.row * 2 + 1];
                quantity2.font = [UIFont systemFontOfSize:15];
                quantity2.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                [cell.contentView addSubview:quantity2];
            }
        } else {
            UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
            material1.text = [_auxiliaryMaterial objectAtIndex:indexPath.row * 2];
            material1.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:material1];
            
            UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(94, 10, 70, 30)];
            quantity1.text = [_auxiliaryQuantity objectAtIndex:indexPath.row * 2];
            quantity1.font = [UIFont systemFontOfSize:15];
            quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
            [cell.contentView addSubview:quantity1];
            
            UILabel *material2 = [[UILabel alloc]initWithFrame:CGRectMake(186, 10, 70, 30)];
            material2.text = [_auxiliaryMaterial objectAtIndex:indexPath.row * 2 + 1];
            material2.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:material2];
            
            UILabel *quantity2 = [[UILabel alloc]initWithFrame:CGRectMake(251, 10, 70, 30)];
            quantity2.text = [_auxiliaryQuantity objectAtIndex:indexPath.row * 2 + 1];
            quantity2.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
            quantity2.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:quantity2];
        }
    }
    return cell;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
